'use strict';
/*
    koa-router
    是用来处理koa 环境中的路由的,他是 koa 的一个中间件
    app.post
    app.get
    例如这样的函数都是 koa-router 提供的, 调用时必须 app.use(router.routes())
 */
const Koa = require('koa')
const router = require('koa-router')()

const app = new Koa()

app.use(async (ctx, next) => {
    console.log(`${ctx.request.method} ${ctx.request.url}`);
    await next();
})

router.get('/hello/:name', async (ctx, next) => {
    var name = ctx.params.name
    ctx.response.body = `hello ${name}`;
})

router.get(`/`, async (ctx, next) => {
    ctx.response.body = 'index'
})

app.use(router.routes())

app.listen(3333)

console.log('app started ')